package com.zhengtoudai.ui.presenter;

import android.support.annotation.NonNull;

import com.zhengtoudai.ui.view.BaseView;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2016/8/25 0025.
 */

public class BasePresenterImpl<V extends BaseView> implements BasePresenter<V> {
    protected V mView;

    private CompositeSubscription mSubscriptions; //多个请求

    protected Subscription mSubscription; //单个请求

    @Override
    public void attachView(@NonNull V view) {
        mView = view;

    }

    protected void addSubscription(Subscription mSubscription) {
        if (mSubscriptions == null) {
            mSubscriptions = new CompositeSubscription();
        }
        mSubscriptions.add(mSubscription);
    }

    @Override
    public void detachView() {
        if (mSubscription!=null&&!mSubscription.isUnsubscribed()){
            mSubscription.unsubscribe();
        }

        if (mSubscriptions!=null&&mSubscriptions.hasSubscriptions()) {
            mSubscriptions.unsubscribe();
        }

    }
}
